package com.study.ye.controller;

import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


import java.io.File;
import java.io.IOException;
import java.nio.file.Files;

@RequestMapping(value = "/image")
@RestController
public class Test2controller {

    @GetMapping(value = "/a")
    public ResponseEntity<byte[]> getImage() {
        try {
            File file = new File("D:\\uploads\\1.png");
            String imageName = file.getName();
            byte[] imageBytes = Files.readAllBytes(file.toPath());

            HttpHeaders headers = new HttpHeaders();
            headers.setContentType(MediaType.IMAGE_PNG); // 根据图片实际类型调整，如PNG则为MediaType.IMAGE_PNG
            //headers.setContentDispositionFormData("attachment", imageName);
            return ResponseEntity
                    .ok()
                    .headers(headers)
                    .body(imageBytes);
        } catch (IOException e) {
            e.printStackTrace();
            return ResponseEntity.status(500).body(null);
        }
    }
}

